﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/8 星期三 12:54:53
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Sector7.Framework.Utility;
using Sector7.Framework.Consts;

namespace Sector7.Framework.Core
{
    /// <summary>
    /// 配置文件管理类
    /// </summary>
    public class ConfigurationManager
    {
        #region Accessor
        #endregion

        #region Public functions
        /// <summary>
        /// 获取产品配置信息
        /// </summary>
        /// <returns>产品配置信息</returns>
        public ProductConfiguration GetProductConfiguration()
        {
            ProductConfiguration productConfig = null;

            LoadConfiguration();
            if (m_xmlDoc != null)
            {
                productConfig = new ProductConfiguration();

                XmlNode productNode = XmlUtil.GetXmlNode(m_xmlDoc.ChildNodes, C_ConfigNodeName.PRODUCT);
                string propertyName, propertyValue;
                foreach (XmlNode productProperty in XmlUtil.GetXmlNodeList(productNode.ChildNodes, C_ConfigNodeName.PROPERTY))
                {
                    propertyName = XmlUtil.GetXmlNodeAttributeValue(productProperty, C_ConfigAttributeName.NAME);
                    propertyValue = XmlUtil.GetXmlNodeAttributeValue(productProperty, C_ConfigAttributeName.VALUE);

                    switch (propertyName)
                    {
                        case C_ConfigAttributeName.PRODUCT_NAME:
                            productConfig.ProductName = propertyValue;
                            break;
                        case C_ConfigAttributeName.CODE_NAME:
                            productConfig.CodeName = propertyValue;
                            break;
                        case C_ConfigAttributeName.CHINESE_NAME:
                            productConfig.ChineseName = propertyValue;
                            break;
                        case C_ConfigAttributeName.FULL_VERSION:
                            productConfig.FullVersion = propertyValue;
                            break;
                        case C_ConfigAttributeName.SHORT_VERSION:
                            productConfig.ShortVersion = propertyValue;
                            break;
                        default:
                            break;
                    }
                }
            }

            return productConfig;
        }

        /// <summary>
        /// 获取模块配置信息
        /// </summary>
        /// <returns>模块配置信息</returns>
        public List<ModuleConfiguration> GetModuleConfiguration()
        {
            List<ModuleConfiguration> moduleConfigList = new List<ModuleConfiguration>();

            LoadConfiguration();
            if (m_xmlDoc != null)
            {
                foreach (XmlNode moduleNode in XmlUtil.GetXmlNodeList(m_xmlDoc.ChildNodes, C_ConfigNodeName.MODULE))
                {
                    var moduleConfig = new ModuleConfiguration();
                    string propertyName, propertyValue;
                    foreach (XmlNode moduleProperty in XmlUtil.GetXmlNodeList(moduleNode.ChildNodes, C_ConfigNodeName.PROPERTY))
                    {
                        propertyName = XmlUtil.GetXmlNodeAttributeValue(moduleProperty, C_ConfigAttributeName.NAME);
                        propertyValue = XmlUtil.GetXmlNodeAttributeValue(moduleProperty, C_ConfigAttributeName.VALUE);

                        switch (propertyName)
                        {
                            case C_ConfigAttributeName.MODULE_ID:
                                moduleConfig.ModuleId = propertyValue;
                                break;
                            case C_ConfigAttributeName.PATH:
                                moduleConfig.Path = propertyValue;
                                break;
                            default:
                                break;
                        }
                    }

                    moduleConfigList.Add(moduleConfig);
                }
            }

            return moduleConfigList;
        }

        /// <summary>
        /// 获取导航配置信息
        /// </summary>
        /// <returns>导航配置信息</returns>
        public List<NavigationConfiguration> GetNavigationConfiguration()
        {
            var navigationConfigList = new List<NavigationConfiguration>();

            LoadConfiguration();
            if (m_xmlDoc != null)
            {
                var navigationsNode = XmlUtil.GetXmlNode(m_xmlDoc.ChildNodes, C_ConfigNodeName.NAVIGATIONS);
                if(navigationsNode != null)
                {
                    foreach (XmlNode navigationNode in XmlUtil.GetXmlNodeListWithoutChildren(navigationsNode.ChildNodes, C_ConfigNodeName.NAVIGATION))
                    {
                        NavigationConfiguration navigationConfig = GetNavigationConfiguration(navigationNode);

                        navigationConfigList.Add(navigationConfig);
                    }
                }
            }

            return navigationConfigList;
        }
        #endregion

        #region Private functions
        private void LoadConfiguration()
        {
            if (m_xmlDoc == null)
            {
                m_xmlDoc = XmlUtil.Open(m_filePath);
            }
        }

        private NavigationConfiguration GetNavigationConfiguration(XmlNode navigationNode)
        {
            var navigationConfig = new NavigationConfiguration();
            navigationConfig.Caption = XmlUtil.GetXmlNodeAttributeValue(navigationNode, C_ConfigAttributeName.CAPTION);
            navigationConfig.ModuleId = XmlUtil.GetXmlNodeAttributeValue(navigationNode, C_ConfigAttributeName.MODULE_ID);
            navigationConfig.ClassName = XmlUtil.GetXmlNodeAttributeValue(navigationNode, C_ConfigAttributeName.CLASS_NAME);

            var itemsNode =XmlUtil.GetXmlNode(navigationNode.ChildNodes, C_ConfigNodeName.ITEMS);
            if (itemsNode != null)
            {
                navigationConfig.Items = new List<NavigationConfiguration>();

                foreach (XmlNode itemNode in XmlUtil.GetXmlNodeListWithoutChildren(itemsNode.ChildNodes, C_ConfigNodeName.NAVIGATION))
                {
                    navigationConfig.Items.Add(GetNavigationConfiguration(itemNode));
                }
            }

            return navigationConfig;
        }
        #endregion

        #region Structure
        /// <summary>
        /// 创建配置文件管理类对象
        /// </summary>
        /// <param name="filePath">配置文件路径</param>
        public ConfigurationManager(string filePath)
        {
            m_filePath = filePath;
        }
        #endregion

        #region Inner members
        private string m_filePath = null;
        private XmlDocument m_xmlDoc = null;
        #endregion
    }
}
